package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.buz.hjcdriver.bean.AResultBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_banklist.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.adapter.SystemMessageAdapter
import lium.lbs.lbsim.bean.SystemMessageContenBean
import lium.lbs.lbsim.utils.ToastUtils
import lium.lbs.lbsim.utils.callbck.DialogCallback
import org.greenrobot.eventbus.EventBus
import java.util.HashMap
import cn.jpush.im.android.api.event.MessageEvent
import com.buz.hjcdriver.bean.InviteApplyMemberBean
import lium.lbs.lbsim.jgchat.application.GJApp
import lium.lbs.lbsim.listener.SystemMessageEven


class SystemMessageActivity : BaseActivity(), BaseQuickAdapter.OnItemClickListener {


    lateinit var adapter: SystemMessageAdapter
    var pageNo:Int = 1
    override fun getLayoutId(): Int {
        return R.layout.activity_system_message
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("系统消息")
        EventBus.getDefault().register(this)
        initRecyclerView()
        getDataList()
    }

    fun initRecyclerView() {
        //初始化adapter
        adapter = SystemMessageAdapter()
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
       // adapter.disableLoadMoreIfNotFullPage(recyclerView) //关闭BRAVE的自动加载功能 改成 refreshLayout的上拉加载
        refreshLayout.setOnRefreshListener {
            pageNo =1;
            getDataList()
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }
        refreshLayout.setOnLoadMoreListener {
            ++pageNo
            getDataList()
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }
    }

    fun getDataList() {
        val param = HashMap<String, String>()
        param["pageNo"] = pageNo.toString()
        param["pageSize"] = "20"
        postData(
            "/groupmember/applyList",
            param,
            object : DialogCallback<ResponseBean<SystemMessageContenBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<SystemMessageContenBean>>) {
                    setDataList(response.body().data)
                }
            })
    }

    fun setDataList(list: SystemMessageContenBean) {
        refreshLayout.finishRefresh()
        refreshLayout.finishLoadMore()
        if (list == null || list.applyList.isEmpty()) {
            refreshLayout.finishLoadMoreWithNoMoreData()  //没有数据
            if (pageNo>1){
                pageNo--
            }
        } else {
            list?.let {
                if(pageNo == 1){
                    adapter!!.data.clear()
                }
                adapter!!.data.addAll(list.applyList)
            }
        }
        refreshLayout.finishLoadMoreWithNoMoreData()
        adapter!!.notifyDataSetChanged()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

    }

    //个人
    fun getAgreetOrRefuse(id: String, status: String) {
        val param = HashMap<String, String>()
        param["applyId"] = id
        param["status"] = status//2：同意；3：拒绝
        postData(
            "/frinds/frindAddOrRefuse",
            param,
            object : DialogCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    setPersonal(response.body().msg)
                }
            })
    }


    private fun setPersonal(msg: String) {
        ToastUtils.showLongToast(msg)
        getDataList()
        jumpActivity()
    }


    override fun getNetWorkData() {
    }

    //群的
    fun getAgreetQun(id: String, status: String, groupName: String) {
        val param = HashMap<String, String>()
        param["applyId"] = id
        param["status"] = status//2：同意；3：拒绝
        postData(
            "/groupmember/addOrRefuseIntoGroup",
            param,
            object : DialogCallback<ResponseBean<InviteApplyMemberBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<InviteApplyMemberBean>>) {
                    setQunDataList(response.body().data, groupName)
                    getDataList()
                    jumpActivity()
                }
            })
    }

    fun setQunDataList(inviteApplyMemberBean: InviteApplyMemberBean, groupName: String) {
        var intent = Intent()
        when (inviteApplyMemberBean.type) {
            "2" -> {//付费
                intent.setClass(this, InGroupByMoneyActivity::class.java)
                intent.putExtra(GJApp.MY_GROUP_ID, inviteApplyMemberBean.groupId)
                intent.putExtra("applyId", inviteApplyMemberBean.applyId)
                intent.putExtra("orderSn", inviteApplyMemberBean.orderSn)
                intent.putExtra("price", inviteApplyMemberBean.price)
                intent.putExtra("groupName", groupName)
                startActivityForResult(intent, 100)
            }
            "3" -> {//回答问题
                intent.setClass(this, InGroupByQuestionActivity::class.java)
                intent.putExtra(GJApp.MY_GROUP_ID, inviteApplyMemberBean.groupId)
                intent.putExtra("applyId", inviteApplyMemberBean.applyId)
                intent.putExtra("question", inviteApplyMemberBean.question)
                intent.putExtra("groupName", groupName)
                startActivityForResult(intent, 100)
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == 200) {
            pageNo = 1
            getDataList()
            jumpActivity()
        }
    }

    fun jumpActivity() {
        EventBus.getDefault().post(SystemMessageEven("刷新"))
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}
